package com.schoolgard.base;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionContext;

public abstract class BaseAction  implements SessionAware ,ServletRequestAware ,ServletResponseAware{
	
	private static final long serialVersionUID = 201111181348L; 
	
protected static final String SUCCESS = "success";
	
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected Map session;
	
	public void setSession(Map<String,Object> session) {
		this.session = session;
	}
	@Override
	public void setServletRequest(HttpServletRequest request) { //这样注入会有并发问题，json通过response回写的时候会找不到哪个组件发起的请求。
		this.request = request;
	}
	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

   /**
     * 获得当前Action 请求路径
     *
    */
   public String getRequestUrl() {
	   StringBuffer reqUrl = request.getRequestURL();
       return reqUrl!=null?reqUrl.toString():null;
    }

   /**
     * 获得当前Session
     *
    */
   public Map getSession() {
       return ActionContext.getContext().getSession();
    }

   /**
     * 根据指定的页面参数名称，获取页面传递来的参数值
     *
     * @param parameter
     * @return 单个对象
    */
   protected Object getParameterValue(String parameter) {
        Object[] parameterArray = getParamenterArray(parameter);
       if (parameterArray != null && parameterArray.length == 1) {
           return parameterArray[0];
        } else {
           return request.getAttribute(parameter);
        }
    }

   /**
     * 根据指定的页面参数名称，获取页面传递来的参数值
     *
     * @param parameter
     * @return 数组对象
    */
   protected Object[] getParamenterArray(String parameter) {
       return (Object[]) (ActionContext.getContext().getParameters()
                .get(parameter));
    }

   /**
     * 向request对象添加attribute
     *
     * @param key
     * @param value
    */
   public void setRequestAttribute(String key, Object value) {
        request.setAttribute(key, value);
    }

   /**
     * 从session中取值
     * @param key
     * @return
    */
   public Object getSessionObj(Object key) {
       return getSession().get(key);
    }
   
   /**
     * 获得项目rootDir
     *
     * @return
    */
   protected String getRootDir() {
        String workDir = request.getSession()
                .getServletContext().getRealPath("login.jsp");

       int len = workDir.indexOf("login.jsp");
        workDir = workDir.substring(0, len);

        StringBuffer sb = new StringBuffer();
        sb.append(workDir).append("upload").append(File.separator);

       return sb.toString();
    }
   
   /**
     * 获得web应用根目录路径
     * @return
    */
   protected String getWebRootDir() {
        String workDir = request.getSession().getServletContext().getRealPath("index.jsp");
       int len = workDir.indexOf("index.jsp");
        workDir = workDir.substring(0, len);
        StringBuffer sb = new StringBuffer();
        sb.append(workDir);
       return sb.toString();
    }
   
   @SuppressWarnings("unchecked")
   public void setSessionObj(Object key,Object value){
        getSession().put(key, value);
    } 
   
   /**
    * 输出流到response
    * @param response
    * @param content
    */
   protected void writeToResponse(String content)
   {
	   //这里不能用上面的response，有并发问题，//TODO待研究线程问题
	   HttpServletResponse response1 = ServletActionContext.getResponse();
	   response1.setCharacterEncoding("utf-8");
	   response1.setContentType("text/html");
		 PrintWriter printer = null;
		 try {
	 			printer = response1.getWriter();
	 			printer.write(content);
		 } catch (IOException e) {
				e.printStackTrace();
		 }finally{
				printer.flush();
		 }
   }
}
